package com.park.controller;

import com.park.common.bean.ArticleInputView;
import com.park.common.bean.PageBean;
import com.park.common.constant.IDBConstant;
import com.park.platform.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
@RequestMapping("article")
public class ArticleController extends BaseController {

    @Autowired
    private IArticleService articleService;

    @RequestMapping("getArticleIndex")
    public String getArticles(Model model){
        return "articles";
    }

    @RequestMapping("getArticles")
    public String getArticles(ArticleInputView articleInputView, Model model){
        try{
            PageBean pageBean = articleService.getArticles(articleInputView);
            model.addAttribute("pageBean", pageBean);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "articles_child";
    }

    @RequestMapping("getNewsIndex")
    public String getNewsIndex(Model model){
        return "news";
    }

    @RequestMapping("getNewss")
    public String getNewss(ArticleInputView articleInputView, Model model){
        try{
            articleInputView.setServiceType(IDBConstant.LOGIC_STATUS_NO);
            PageBean pageBean = articleService.getArticles(articleInputView);
            model.addAttribute("pageBean", pageBean);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "news_child";
    }

    @RequestMapping("getArticleDetails/{articleId}")
    public String getArticleDetails(@PathVariable("articleId") int articleId, Model model){
        try{
            Map<String, Object> resultMap = articleService.getArticleDetails(articleId);
            model.addAllAttributes(resultMap);
        }catch (Exception e){
            e.printStackTrace();
        }
        String serviceType = articleService.getArticleInfo(articleId).getServiceType();
        return IDBConstant.LOGIC_STATUS_YES.equals(serviceType) ? "articles_detail" : "news_detail";
    }

}
